package com.gxa.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.business.dto.BusinessDTO;
import com.gxa.business.dto.EditBusinessDTO;
import com.gxa.business.entity.Business;
import com.gxa.business.mapper.BusinessMapper;
import com.gxa.business.service.BusinessService;
import com.gxa.business.vo.BusinessVO;
import com.gxa.business.vo.QueryBusinessVO;
import com.gxa.common.base.RespResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Slf4j
public class BusinessServiceImpl implements BusinessService {

    @Resource
    private BusinessMapper businessMapper;

    @Override
    public RespResult insertBusinessInfo(BusinessDTO businessDTO) {
        Business business =  BeanUtil.copyProperties(businessDTO, Business.class);
        businessMapper.insert(business);
        return RespResult.success("新建商海成功！");
    }

    @Override
    public RespResult editBusinessInfo(EditBusinessDTO editBusinessDTO) {
        Business business =  BeanUtil.copyProperties(editBusinessDTO, Business.class);
        businessMapper.editBusinessInfo(editBusinessDTO);
        return RespResult.success("编辑商海成功！");
    }

    @Override
    public RespResult updateStatus(Integer code) {
        QueryWrapper<Business> businessQueryWrapper = new QueryWrapper<>();
        businessQueryWrapper.eq("code",code);
        businessMapper.delete(businessQueryWrapper);
        return RespResult.success("下架成功！");
    }

    @Override
    @Transactional
    public RespResult selectBusinessInfoByCode(Integer code) {
        //查询对应商海信息
        BusinessVO businessVO =  businessMapper.selectBusinessInfoByCode(code);
        //浏览量+1
        businessMapper.addPageViews(code);
        return RespResult.success(businessVO);
    }

    @Override
    public RespResult queryAllBusinessInfo(int currentPage, int pageSize) {
        Page<QueryBusinessVO> businessVOPage = new Page<>(currentPage,pageSize);
        IPage<QueryBusinessVO> businessVOIPage = businessMapper.queryAllBusinessInfo(businessVOPage);
        return RespResult.success(businessVOIPage.getRecords());
    }

    @Override
    public RespResult queryAllBusinessInfoByCountry(int currentPage, int pageSize, String country) {
        Page<QueryBusinessVO> businessVOPage = new Page<>(currentPage,pageSize);
        IPage<QueryBusinessVO> businessVOIPage = businessMapper.queryAllBusinessInfoByCountry(businessVOPage,country);
        return RespResult.success(businessVOIPage.getRecords());
    }
}
